package visiopuce.ui;

import java.util.Locale;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.nebula.widgets.formattedtext.FormattedText;
import org.eclipse.nebula.widgets.formattedtext.NumberFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import visiopuce.ApplicationContexte;

public class NbDialog extends Dialog {
	
	
	private FormattedText tNombre;
	private Button btnOk;
	private Button btnAnnuler;
	String titre;
	private String question;
	private Integer nombre;


	public NbDialog(Shell parentShell, String titre, String question) {
		super(parentShell);
		this.titre = titre;
		this.question = question;
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER
				| SWT.APPLICATION_MODAL);
		setBlockOnOpen(true);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(titre);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(1, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		Label lblQuestion = new Label(container, SWT.NONE);
		lblQuestion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblQuestion.setText(question);

		tNombre = new FormattedText(container, SWT.BORDER);
		tNombre.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier,Locale.FRENCH));
		tNombre.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Ok", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(255, 198);
	}

	@Override
	protected void okPressed() {
		try { 
			nombre = ((Long)tNombre.getValue()).intValue();
	    } catch(NumberFormatException e) { 
	    	nombre = null; 
	    }
		
		super.okPressed();
	}

	public Integer getNombre() {
		return nombre;
	}
	
	

}